﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Logging;

namespace MMITIntegration
{
    
    public class UkashOutput
    {
        private string txCode = "";

        public string TxCode
        {
            get { return txCode; }
            set { txCode = value; }
        }
        private string txDescription = "";

        public string TxDescription
        {
            get { return txDescription; }
            set { txDescription = value; }
        }
        private string settleAmount = "";

        public string SettleAmount
        {
            get { return settleAmount; }
            set { settleAmount = value; }
        }
        private string transactionId = "";

        public string TransactionId
        {
            get { return transactionId; }
            set { transactionId = value; }
        }
        private string changeIssueVoucherNumber = "";

        public string ChangeIssueVoucherNumber
        {
            get { return changeIssueVoucherNumber; }
            set { changeIssueVoucherNumber = value; }
        }
        private string changeIssueVoucherCurr = "";

        public string ChangeIssueVoucherCurr
        {
            get { return changeIssueVoucherCurr; }
            set { changeIssueVoucherCurr = value; }
        }
        private string changeIssueAmount = "";

        public string ChangeIssueAmount
        {
            get { return changeIssueAmount; }
            set { changeIssueAmount = value; }
        }
        private string changeIssueExpiryDate = "";

        public string ChangeIssueExpiryDate
        {
            get { return changeIssueExpiryDate; }
            set { changeIssueExpiryDate = value; }
        }
        private string ukashTransactionId = "";

        public string UkashTransactionId
        {
            get { return ukashTransactionId; }
            set { ukashTransactionId = value; }
        }
        private string currencyConversion = "";

        public string CurrencyConversion
        {
            get { return currencyConversion; }
            set { currencyConversion = value; }
        }
        private string errCode = "";

        public string ErrCode
        {
            get { return errCode; }
            set { errCode = value; }
        }
        private string errDescription = "";

        public string ErrDescription
        {
            get { return errDescription; }
            set { errDescription = value; }
        }

        public override String ToString()
        {
            string outputXml = "";

            outputXml = string.Format("<UKashTransaction><txCode>{0}</txCode><txDescription>{1}</txDescription><settleAmount>{2}</settleAmount><transactionId>{3}</transactionId><changeIssueVoucherNumber>{4}</changeIssueVoucherNumber><changeIssueVoucherCurr>{5}</changeIssueVoucherCurr><changeIssueAmount>{6}</changeIssueAmount><changeIssueExpiryDate>{7}</changeIssueExpiryDate><ukashTransactionId>{8}</ukashTransactionId><currencyConversion>{9}</currencyConversion><errCode>{10}</errCode><errDescription>{11}</errDescription></UKashTransaction>",
                TxCode,
                TxDescription,
                SettleAmount,
                TransactionId,
                ChangeIssueVoucherNumber,
                ChangeIssueVoucherCurr,
                ChangeIssueAmount,
                ChangeIssueExpiryDate,
                UkashTransactionId,
                CurrencyConversion,
                ErrCode,
                ErrDescription);

            return outputXml;
        }

        public static UkashOutput FromString(string Xml)
        {
           
            // assume input element is an XML, in other case an exception will be thrown and captured
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(Xml);

            UkashOutput bean = new UkashOutput();


            //Loggin Raw XML from UKASH
            Dictionary<string, object> d = new Dictionary<string, object>();
            SynkronLogger Log = new SynkronLogger("MMITCore");
            

            d.Add("XML recieved from UKASH", Xml);
            Log.WriteInformation(56421722, "XML recieved from UKASH", d);
            // iterate over nodes in the document assuming they are children on the first child
            foreach (XmlNode node in doc.FirstChild.ChildNodes)
            {
                switch (node.Name)
                {
                    case "txCode":
                        bean.TxCode = node.InnerText;
                        break;

                    case "txDescription":
                        bean.TxDescription = node.InnerText;
                        break;

                    case "settleAmount":
                        bean.SettleAmount = node.InnerText;
                        break;

                    case "transactionId":
                        bean.TransactionId = node.InnerText;
                        break;

                    case "changeIssueVoucherNumber":
                        bean.ChangeIssueVoucherNumber = node.InnerText;
                        break;

                    case "changeIssueVoucherCurr":
                        bean.ChangeIssueVoucherCurr = node.InnerText;
                        break;

                    case "changeIssueAmount":
                        bean.ChangeIssueAmount = node.InnerText;
                        break;

                
                    case "changeIssueExpiryDate":
                        bean.ChangeIssueExpiryDate = node.InnerText;
                        break;

                    case "ukashTransactionId":
                        bean.UkashTransactionId = node.InnerText;
                        break;

                    case "currencyConversion":
                        bean.CurrencyConversion = node.InnerText;
                        break;

                    case "errCode":
                        bean.ErrCode = node.InnerText;
                        break;

                    case "errDescription":
                        bean.ErrDescription = node.InnerText;
                        break;
                }
            }

            return bean;
        }
    }
    
}